<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<%@ taglib prefix="codec" uri="/WEB-INF/tld/codec.tld"%>
<%@ include file="/WEB-INF/common/taglibs.jsp"%>
<script type="text/javascript" src="${ctx}/resources/js/datatable-query.js"></script>

<div class="row-fluid">
 
	<div class="span12">
	<div class="box gradient">
	 
		<div class="title">
				<h4>
				<span>Template Snap Shot </span> <span class="box-form right"> </span>
				
				<span class="box-form right templateSnapShot_bar"><a href="#" class="btn btn-mini addTemplateSnapShot">Add Template Snap Shot</a></span>
 				</h4>
 				<a href="#" class="minimize"> </a>
			</div>
		 <div class="content scrollable clearfix">
		<table class="table table-bordered table-vertical-centre templateSnapShotTable">
			<thead>
				<tr>
					<th><a class="select_all"><span class="icomoon-icon-checkmark-2"></span></a></th>
<th>Name </th>
<th>Version </th>
<th>Date </th>
 					<th>Action</th>
				</tr>
			</thead>
		</table>
		</div>
	 
</div>
 </div>





<script>
 
 

$(document).ready(function(){
	var del = {
			id : 'templateSnapShotDel',
			text  : 'Del',
			domain : "Template Snap Shot",
			operation : "Del",
			url : "${ctx}/admin/template-snap-shot/del/",
			selector : ".templateSnapShot_bar",
			selects  : $('.templateSnapShotTable'),
	  
	};
	johnny.tableBtn(del);
	
	
	var apply = {
			id : 'Apply',
			text  : 'Apply',
			domain : "Template Snap Shot",
			operation : "Apply ",
			url : "${ctx}/admin/template-snap-shot/apply/",
			max : 1,
			selector : ".templateSnapShot_bar",
			selects  : $('.templateSnapShotTable'),
	  
	};
	johnny.tableBtn(apply);
	
 	
	
	$('.addTemplateSnapShot').click(function(){
	$("#templateSnapShotDiv").find("div.modal-body").find("input,select,textarea,checkbox").not("[name=action],[type=hidden]").not("[type=radio]").each(function(step,value){
										if($(this).is("select")){
											var d = $(value).find("option:first").val();
											$(this).val(d);
										}else{
		 									$(value).val("");
										}
										if(!$(value).hasClass("nostyle")){
											$(value).uniform();
										}
	 							});
	
	
	
	 $("#templateSnapShotDiv input[name='action']").val("${ctx}/admin/template-snap-shot/${entity.id}/create/");
 	 $("#templateSnapShotDiv").modal('show');
  });
	
	  $(document).delegate('.templateSnapShotTable .icomoon-icon-pencil', 'click', function(event) {
		  
		  	var id = $(this).parent().attr("dataid");
			$.ajax({
				url : "${ctx}/admin/template-snap-shot/"+id+"/edit/",
				type : "GET",
				dataType : "json",
				success : function(data){		
					if(data && data.state && (data.state.indexOf("ERROR") != -1)){
						johnny.error(data.message);
						return;
					}else{
							var entity = data.entity;
							
							
							
 							$("#templateSnapShotDiv").find("div.modal-body").find("input[name],select[name],textarea[name],checkbox[name]").not("[name=action],[type=hidden]").each(function(step,value){
									var name = $(value).attr("name");
									if ($(value).is(':radio')) {
										if (""+entity[name] ===""+$(value).val()){
  												$(value).attr("checked","checked");
   											}else{
   												$(value).removeAttr("checked");
 																			
 											}
								 		var css =  $(":checkbox[fname="+name+"]");
										if (css.length>0){
											if ("true" === ""+entity[name]){
												if (!css.attr("checked")){
													css.siblings("label").trigger('mousedown')
									                  .trigger('mouseup')
									                  .trigger('click');
												}
											}else{
												if (css.attr("checked")){
													css.siblings("label").trigger('mousedown')
									                  .trigger('mouseup')
									                  .trigger('click');
												}

												
												
											}
										}
										
									}else{
 									if (entity[name]|| entity[name]===0){
										$(value).val(entity[name]);
 										if ($(value).hasClass("nostyle")){}else{$(value).uniform();}
 									}else if (name.match(/Id$/)){
										var pro = name.substring(0,name.length-2);
										if (entity[pro] &&entity[pro].id){
											$(value).val(entity[pro].id);
	 										if ($(value).hasClass("nostyle")){}else{$(value).uniform();}
 										}
 									
									}
									else{
 										$(value).val("");
									}}
								
							});
							
							var x = $("#templateSnapShotDiv").find("div.modal-body").find('input[type="radio"]').not('.nostyle').uniform();
   							$("#templateSnapShotDiv input[name='action']").val("${ctx}/admin/template-snap-shot/"+id+"/edit/");
     	    				$("#templateSnapShotDiv").modal('show');
 					}
				}
			});		  
 
	     
	    });
	var recordColumn = ['','name','version','date'];
	
	 $('.templateSnapShotTable').dataTable({
		"bLengthChange" : true,
		"aoColumnDefs" : [ {
			'bSortable' : false,
			'aTargets' : [0,4 ]
		} ],
 		"sAjaxSource" : '${ctx}/admin/template-snap-shot/${entity.id}/',
		"createQueryData" : function (aoData){
				return	johnny.initQueryData(aoData, recordColumn);
		},
		"createResult" : function (json, echo){
 			var dataArray = new Array();
			for ( var i = 0; i < json.result.length; i++) {
				var subArray = new Array();
				var value = johnny.getForSelect(json.result[i].id);
				subArray.push(value);
				subArray.push(json.result[i].name)
				subArray.push(json.result[i].version)
				subArray.push(json.result[i].date)
				
				var preview = "${ctx}/admin/template-snap-shot/" + json.result[i].id + "/preview/";

   				var conf = [{
					link : 'javascript:void(1)',
					dataId : json.result[i].id,
					text : 'edit',
					css  : 'icomoon-icon-pencil'
				},{
					link : preview,
					text : 'preview',
					css   : 'icomoon-icon-copy-2'
					
				} 
				
				];
				subArray.push(johnny.getGridLink(conf)); 
  				dataArray.push(subArray);
			}
			return {
				sEcho : echo,
				iTotalRecords : json.totalCount,
				iTotalDisplayRecords : json.totalCount,
				aaData : dataArray
			};
		}		
	});
	 
	
	
	
	
});


</script>





<div id="templateSnapShotDiv" class="modal hide fade"  selector=".templateSnapShotTable" style="display: none; ">
	<div class="modal-header">
		<button type="button" class="close" data-dismiss="modal">
			<span class="icon12 minia-icon-close"></span>
		</button>
		<h3>	Template Snap Shot  </h3>
	</div>
 
	<div class="modal-body">
  						<input type="hidden" name="action" >
<!--name --><div class="form-row row-fluid"><div class="span12"><div class="row-fluid"><label class="form-label span3" for="name">Name :</label><input class="span9" id="name" name="name"   type="text" value="" /></div></div></div>
<!--version --><div class="form-row row-fluid"><div class="span12"><div class="row-fluid"><label class="form-label span3" for="version">Version :</label><input class="span9" id="version" name="version"   type="text" value="" /></div></div></div>
<!--date --><div class="form-row row-fluid"><div class="span12"><div class="row-fluid"><label class="form-label span3" for="date">Date :</label><input class="span4 datepicker" id="date" name="date" type="text" value="" /></div></div></div>

						 
	</div>
	 
	<div class="modal-footer">
		<a href="#" class="btn" data-dismiss="modal">Close</a>
		<a href="#" class="btn btn-primary modal-submit">Save changes</a>
	</div>

</div>





</div>
	